package cn.toprog.viewer.component;

import java.io.File;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;

/**
 * 项目名称: TaoImagerViewer
 * 创建时间: 2021/4/8 11:44
 * 作者姓名: xiaolei
 */

/**
 * 缩略图类
 */
public class Thumbnail extends Label {
    private static final double LARGE_ICON_WIDTH = 200;
    private static final double LARGE_ICON_HEIGHT = 230;

    public static String BG_NORMAL = "-fx-background-color:white; -fx-padding: 5px;";
    public static String BG_HOVER = "-fx-background-color:lightblue; -fx-padding: 5px;";
    public static String BG_SELECTED = "-fx-background-color:#4ef2; -fx-padding: 5px;";

    private File imageFile;
    private boolean selected;

    public Thumbnail(File imageFile) {
        this.imageFile = imageFile;
        this.setPrefSize(LARGE_ICON_WIDTH + 10, LARGE_ICON_HEIGHT + 10);
        this.setStyle(BG_NORMAL);
        this.setGraphicTextGap(5.0);
        this.setContentDisplay(ContentDisplay.TOP);
        this.setAlignment(Pos.BOTTOM_CENTER);

        this.setText(imageFile.getName());
        this.setGraphic(new ImageView(loadImage(LARGE_ICON_WIDTH, LARGE_ICON_WIDTH)));

    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
        if (isSelected()) {
            this.setStyle(BG_SELECTED);
        } else {
            this.setStyle(BG_NORMAL);
        }
    }

    public File getImageFile() {
        return imageFile;
    }

    private Image loadImage(double w, double h) {
        Image image = new Image(imageFile.toURI().toString(), w, h, true, true);
        return image;
    }
}
